package com.suning.sawp.web.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.google.common.collect.ForwardingList;

public abstract class TotalAvgListDecoraor<T> extends ForwardingList<T> {

    protected Long[] totalavg(Long[] records) {
        Long total = 0L;
        Long avg = 0L;
        for (Long num : records) {
            if (null == num) {
                // 忽略空值
                continue;
            }
            total += num;
        }
        avg = records.length > 0 ? total / records.length : 0;
        return new Long[] { total, avg };
    }

    protected Integer[] totalavg(Integer[] records) {
        Integer total = 0;
        Integer avg = 0;
        for (Integer num : records) {
            if (null == num) {
                // 忽略空值
                continue;
            }
            total += num;
        }
        avg = records.length > 0 ? total / records.length : 0;
        return new Integer[] { total, avg };
    }

    protected BigDecimal[] totalavg(BigDecimal[] records) {
        BigDecimal total = new BigDecimal(0);
        BigDecimal avg = new BigDecimal(0);
        BigDecimal demo = null;
        for (BigDecimal num : records) {
            if (null == num) {
                // 忽略空值
                continue;
            }
            demo = (null == demo) ? num : demo;
            total = total.add(num);
        }
        if (null != demo) {
            avg = total.divide(BigDecimal.valueOf(records.length), demo.scale(), RoundingMode.HALF_DOWN);
            avg = avg.setScale(demo.scale(), RoundingMode.HALF_DOWN);
        }
        return new BigDecimal[] { total, avg };
    }
}
